/*
 * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package jsr335.agent;

import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;

/**
 * @author Remi Forax, Brian Goetz
 */
public class DefenderAttribute extends Attribute {
    private final String className;
    private final String name;
    private final String descriptor;

    static final DefenderAttribute PROTOTYPE = new DefenderAttribute(null, null, null);

    private DefenderAttribute(String className, String name, String descriptor) {
        super("Defender");
        this.className = className;
        this.name = name;
        this.descriptor = descriptor;
    }

    public String getClassName() {
        return className;
    }

    public String getName() {
        return name;
    }

    public String getDescriptor() {
        return descriptor;
    }

    @Override
    public int hashCode() {
        return className.hashCode() ^
               Integer.rotateLeft(8, name.hashCode()) ^
               Integer.rotateLeft(24, descriptor.hashCode());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof DefenderAttribute)) {
            return false;
        }
        DefenderAttribute attribute = (DefenderAttribute) obj;
        return className.equals(attribute.className) &&
               name.equals(attribute.name) &&
               descriptor.equals(attribute.descriptor);
    }

    @Override
    public String toString() {
        return className + '.' + name + descriptor;
    }

    @Override
    protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
        if (len != 4)
            throw new ClassFormatError();

        String className = cr.readClass(off, buf);
        int nameAndTypeIndex = cr.getItem(cr.readUnsignedShort(off + 2));
        String name = cr.readUTF8(nameAndTypeIndex, buf);
        String desc = cr.readUTF8(nameAndTypeIndex + 2, buf);
        return new DefenderAttribute(className, name, desc);
    }

    @Override
    protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
        throw new UnsupportedOperationException();
    }
}